{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [IPython帮助和文档](01.01-Help-And-Documentation.ipynb) | [目录](Index.ipynb) | [IPython魔术命令](01.03-Magic-Commands.ipynb) >\n",
    "\n",
    "<a href=\"https://colab.research.google.com/github/wangyingsm/Python-Data-Science-Handbook/blob/master/notebooks/01.02-Shell-Keyboard-Shortcuts.ipynb\"><img align=\"left\" src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\" title=\"Open and Execute in Google Colaboratory\"></a>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Keyboard Shortcuts in the IPython Shell\n",
    "\n",
    "# IPython Shell中的键盘快捷键"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> If you spend any amount of time on the computer, you've probably found a use for keyboard shortcuts in your workflow.\n",
    "Most familiar perhaps are the Cmd-C and Cmd-V (or Ctrl-C and Ctrl-V) for copying and pasting in a wide variety of programs and systems.\n",
    "Power-users tend to go even further: popular text editors like Emacs, Vim, and others provide users an incredible range of operations through intricate combinations of keystrokes.\n",
    "\n",
    "如果你已经使用计算机一段时间了，你会发现键盘快捷键在你的工作中经常会被用到。这里面最常用的莫过于Cmd-C和Cmd-V（或者Ctrl-C和Ctrl-V），用来复制和粘贴。熟练的用户可能走得更远：流行的文本编辑器如Emacs、Vim等会给用户提供很多的组合快捷键。\n",
    "\n",
    "> The IPython shell doesn't go this far, but does provide a number of keyboard shortcuts for fast navigation while typing commands.\n",
    "These shortcuts are not in fact provided by IPython itself, but through its dependency on the GNU Readline library: as such, some of the following shortcuts may differ depending on your system configuration.\n",
    "Also, while some of these shortcuts do work in the browser-based notebook, this section is primarily about shortcuts in the IPython shell.\n",
    "\n",
    "IPython shell没有像上述的文本编辑器那么复杂，但是也提供了不少的快捷键能让用户在输入命令的时候提高工作效率。这些快捷键实际上并不是IPython本身提供的，是基于它所依赖的GNU Readline库提供的：因此，下面介绍的某些快捷键可能会根据你的系统设置不同而发生改变。虽然本小节介绍的一些快捷键也在浏览器中的notebook应用，但是目前我们聚焦在IPython shell上。\n",
    "\n",
    "> Once you get accustomed to these, they can be very useful for quickly performing certain commands without moving your hands from the \"home\" keyboard position.\n",
    "If you're an Emacs user or if you have experience with Linux-style shells, the following will be very familiar.\n",
    "We'll group these shortcuts into a few categories: *navigation shortcuts*, *text entry shortcuts*, *command history shortcuts*, and *miscellaneous shortcuts*.\n",
    "\n",
    "一旦你习惯了这些快捷键，你会发现它们能大大提高你在shell中输入命令的效率，甚至在你的手指不需要离开键盘主位置的情况下。如果你是一个Emacs编辑器的用户，或者是一个Linux shell的用户，以下内容对你来说不会陌生。我们将这些快捷键分为几组：*导航快捷键*, *文字输入快捷键*, *命令历史实现快捷键*以及*杂项快捷键*。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Navigation shortcuts\n",
    "\n",
    "## 导航快捷键\n",
    "\n",
    "> While the use of the left and right arrow keys to move backward and forward in the line is quite obvious, there are other options that don't require moving your hands from the \"home\" keyboard position:\n",
    "\n",
    "> | Keystroke                         | Action                                     |\n",
    "|-----------------------------------|--------------------------------------------|\n",
    "| ``Ctrl-a``                        | Move cursor to the beginning of the line   |\n",
    "| ``Ctrl-e``                        | Move cursor to the end of the line         |\n",
    "| ``Ctrl-b`` or the left arrow key  | Move cursor back one character             |\n",
    "| ``Ctrl-f`` or the right arrow key | Move cursor forward one character          |\n",
    "\n",
    "显然使用左右箭头键来在一行命令中前后移动是很明显的，但是也有其他的选择让你无需将手移动到主键盘位置之外：\n",
    "\n",
    "| 按键                         | 动作                                     |\n",
    "|-----------------------------------|--------------------------------------------|\n",
    "| ``Ctrl-a``                        | 将光标移动到本行开始位置   |\n",
    "| ``Ctrl-e``                        | 将光标移动到本行结束位置         |\n",
    "| ``Ctrl-b`` 或者 左箭头  | 将光标向左移动一个字符             |\n",
    "| ``Ctrl-f`` 或者 右箭头  | 将光标向右移动一个字符          |\n",
    "\n",
    "译者注：如果你熟悉BASH，这四个快捷键一定不陌生。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Text Entry Shortcuts\n",
    "\n",
    "## 文字输入快捷键\n",
    "\n",
    "> While everyone is familiar with using the Backspace key to delete the previous character, reaching for the key often requires some minor finger gymnastics, and it only deletes a single character at a time.\n",
    "In IPython there are several shortcuts for removing some portion of the text you're typing.\n",
    "The most immediately useful of these are the commands to delete entire lines of text.\n",
    "You'll know these have become second-nature if you find yourself using a combination of Ctrl-b and Ctrl-d instead of reaching for Backspace to delete the previous character!\n",
    "\n",
    "> | Keystroke                     | Action                                           |\n",
    "|-------------------------------|--------------------------------------------------|\n",
    "| Backspace key                 | Delete previous character in line                |\n",
    "| ``Ctrl-d``                    | Delete next character in line                    |\n",
    "| ``Ctrl-k``                    | Cut text from cursor to end of line              |\n",
    "| ``Ctrl-u``                    | Cut text from beginning of line to cursor        |\n",
    "| ``Ctrl-y``                    | Yank (i.e. paste) text that was previously cut   |\n",
    "| ``Ctrl-t``                    | Transpose (i.e., switch) previous two characters |\n",
    "\n",
    "我们都知道使用回退键可以删除前一个字符，去按下这个键有时也需要将手移出主键盘位置，而且这个键每次只能删除一个字符。在IPython中，有一些快捷键可以删除部分你正在输入的文字。这其中最有用的可能就是删除整行文字。当你熟练之后，你也可能本能的使用Ctrl-b和Ctrl-d来代替回退键。\n",
    "\n",
    "| 按键                     | 动作                                           |\n",
    "|-------------------------------|--------------------------------------------------|\n",
    "| 回退键                 | 删除光标前一个字符                |\n",
    "| ``Ctrl-d`` 或者 删除键 | 删除光标所在字符                    |\n",
    "| ``Ctrl-k``                    | 剪切光标所在位置直至末尾的字符              |\n",
    "| ``Ctrl-u``                    | 剪切开头直至光标所在前一个位置的字符        |\n",
    "| ``Ctrl-y``                    | 粘贴字符到光标所在位置   |\n",
    "| ``Ctrl-t``                    | 交换光标前一位置和光标所在位置的字符 |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Command History Shortcuts\n",
    "\n",
    "## 命令历史快捷键\n",
    "\n",
    "> Perhaps the most impactful shortcuts discussed here are the ones IPython provides for navigating the command history.\n",
    "This command history goes beyond your current IPython session: your entire command history is stored in a SQLite database in your IPython profile directory.\n",
    "The most straightforward way to access these is with the up and down arrow keys to step through the history, but other options exist as well:\n",
    "\n",
    "> | Keystroke                           | Action                                     |\n",
    "|-------------------------------------|--------------------------------------------|\n",
    "| ``Ctrl-p`` (or the up arrow key)    | Access previous command in history         |\n",
    "| ``Ctrl-n`` (or the down arrow key)  | Access next command in history             |\n",
    "| ``Ctrl-r``                          | Reverse-search through command history     |\n",
    "\n",
    "本小节讨论的快捷键中，可能提供在命令历史中导航的快捷键最令人震撼。命令的历史不仅仅是当前的IPython会话有效，所有的命令历史都会被记录到一个SQLite的数据库中，保存在你的IPython配置目录下。最直接使用命令历史的方法就是向上的箭头和向下的箭头，下表列示了命令历史的快捷键：\n",
    "\n",
    "| 按键                           | 动作                                     |\n",
    "|-------------------------------------|--------------------------------------------|\n",
    "| ``Ctrl-p`` 或者 上箭头    | 获取上一条命令历史         |\n",
    "| ``Ctrl-n`` 或者 下箭头  | 获取下一条命令历史             |\n",
    "| ``Ctrl-r``                          | 反向搜索命令历史     |"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> The reverse-search can be particularly useful.\n",
    "Recall that in the previous section we defined a function called ``square``.\n",
    "Let's reverse-search our Python history from a new IPython shell and find this definition again.\n",
    "When you press Ctrl-r in the IPython terminal, you'll see the following prompt:\n",
    "\n",
    "反向搜索有时会非常有用。回忆一下上一节中我们定义了一个函数名叫`square`。让我们在IPython shell中使用命令历史回查这个函数的定义。当我们在IPython终端中按下Ctrl-r时，你会看到如下的提示符：\n",
    "\n",
    "```python\n",
    "In [1]:\n",
    "(reverse-i-search)`': \n",
    "```\n",
    "\n",
    "> If you start typing characters at this prompt, IPython will auto-fill the most recent command, if any, that matches those characters:\n",
    "\n",
    "如果你在这个提示符下输入，IPython会根据你输入的部分内容自动补充最近使用的命令：\n",
    "\n",
    "```python\n",
    "In [1]: \n",
    "(reverse-i-search)`sqa': square??\n",
    "```\n",
    "\n",
    "> At any point, you can add more characters to refine the search, or press Ctrl-r again to search further for another command that matches the query. If you followed along in the previous section, pressing Ctrl-r twice more gives:\n",
    "\n",
    "在这种情况下，你还可以输入更多的字符来精准搜索，或者继续按键Ctrl-r来查找下一个（更早）能匹配的命令。如果你输入了上例中的`sqa`，再按下一次Ctrl-r会得到：\n",
    "\n",
    "```python\n",
    "In [1]: \n",
    "(reverse-i-search)`sqa': def square(a):\n",
    "    \"\"\"Return the square of a\"\"\"\n",
    "    return a ** 2\n",
    "```\n",
    "\n",
    "> Once you have found the command you're looking for, press Return and the search will end.\n",
    "We can then use the retrieved command, and carry-on with our session:\n",
    "\n",
    "一旦你找到了你需要的命令，敲击回车将结束反向搜索。然后你就能使用找到的命令继续了：\n",
    "\n",
    "\n",
    "```python\n",
    "In [1]: def square(a):\n",
    "    \"\"\"Return the square of a\"\"\"\n",
    "    return a ** 2\n",
    "\n",
    "In [2]: square(2)\n",
    "Out[2]: 4\n",
    "```\n",
    "\n",
    "> Note that Ctrl-p/Ctrl-n or the up/down arrow keys can also be used to search through history, but only by matching characters at the beginning of the line.\n",
    "That is, if you type **``def``** and then press Ctrl-p, it would find the most recent command (if any) in your history that begins with the characters ``def``.\n",
    "\n",
    "注意Ctrl-p/Ctrl-n或者上箭头/下箭头键也可以用来进行反向搜索，但是仅能匹配命令开头的那些字符。意思是，如果你输入**`def`**然后键入Ctrl-p或者向上箭头，IPython会试图寻找最新的一条命令历史，并且以`def`开头。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Miscellaneous Shortcuts\n",
    "\n",
    "## 杂项快捷键\n",
    "\n",
    "> Finally, there are a few miscellaneous shortcuts that don't fit into any of the preceding categories, but are nevertheless useful to know:\n",
    "\n",
    "> | Keystroke                     | Action                                     |\n",
    "|-------------------------------|--------------------------------------------|\n",
    "| ``Ctrl-l``                    | Clear terminal screen                      |\n",
    "| ``Ctrl-c``                    | Interrupt current Python command           |\n",
    "| ``Ctrl-d``                    | Exit IPython session                       |\n",
    "\n",
    "最后，还有一些杂项的快捷键不属于上述的组别中，但是也挺有用：\n",
    "\n",
    "| 按键                     | 动作                                     |\n",
    "|-------------------------------|--------------------------------------------|\n",
    "| ``Ctrl-l``                    | 清除终端窗口内容                      |\n",
    "| ``Ctrl-c``                    | 终止当前的Python语句执行           |\n",
    "| ``Ctrl-d``                    | 退出IPython会话                       |\n",
    "\n",
    "> The Ctrl-c in particular can be useful when you inadvertently start a very long-running job.\n",
    "\n",
    "Ctrl-c当你在不小心运行了一个非常花时间（或者无限循环）的任务时会很有用。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> While some of the shortcuts discussed here may seem a bit tedious at first, they quickly become automatic with practice.\n",
    "Once you develop that muscle memory, I suspect you will even find yourself wishing they were available in other contexts.\n",
    "\n",
    "虽然本节列出的快捷键看起来很冗余，但是很快你会发现它们在实践中的作用。一旦你形成了肌肉记忆，你甚至会希望在其他环境中也能使用它们。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!--NAVIGATION-->\n",
    "< [IPython帮助和文档](01.01-Help-And-Documentation.ipynb) | [目录](Index.ipynb) | [IPython魔术命令](01.03-Magic-Commands.ipynb) >\n",
    "\n",
    "<a href=\"https://colab.research.google.com/github/wangyingsm/Python-Data-Science-Handbook/blob/master/notebooks/01.02-Shell-Keyboard-Shortcuts.ipynb\"><img align=\"left\" src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open in Colab\" title=\"Open and Execute in Google Colaboratory\"></a>"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
